package cn.cinz.controller;

import cn.cinz.dao.SendSmsMapper;
import cn.cinz.pojo.Code;
import cn.cinz.service.SendSmsService;
import cn.cinz.service.UserService;
import cn.cinz.util.SendSmsUtil;
import com.alibaba.fastjson.JSON;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * 用户数据 返回json数据
 */

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserJsonController {

    //controller 调service层
    @Autowired //它可以对类成员变量、方法及构造函数进行标注，完成自动装配的工作  找到BookService接口 在xml中找到BookService的实现类，完成注入
    @Qualifier("UserServiceImpl") //接口有多个实现类，@Qualifier指明@Autowired具体注入哪个实现类
    private UserService userService;

    @Autowired
    @Qualifier("SendSmsServiceImpl")
    private SendSmsService sendSmsService;


    //private String code = SendSmsUtil.code;//验证码值

    /**
     * 获取已登录用户的信息
     *
     * @return 返回用户信息
     */
    @RequestMapping("/getSessionName")
    public String getName(HttpSession session) {
        String msg = "";
        String userName = String.valueOf(session.getAttribute("userName"));
        if (userName != null) {
            return msg = userName;
        } else {
            //session回收
            session.removeAttribute("userName");
            return msg = "session获取失败";
        }
    }

    /**
     * 获取已登录的用户信息
     * @param session
     * @return 返回用户信息
     */
    @RequestMapping("/getSessionUser")
    public String getUser(HttpSession session){
        String msg = "";
        String user = String.valueOf(session.getAttribute("user"));

        if (user != null){
            return JSON.toJSONString(user);
        }else {
            return msg = "session获取失败";
        }
    }

    //注册判断 用户名是否被注册 手机号是否被注册
    @RequestMapping(value = "/userNameTest", produces = {"application/test;charset=UTF-8"})
    public String registerUserNameTest(@Param("userName") String userName, @Param("userPhone") String userPhone) {
        String UserNameMsg = "";
        int flag = 0;

        //判断用户名是否存在相同   不能提交空白
        if (userName != null && !"".equals(userName.trim())) {
            flag = userService.addQueryUser(userName, userPhone);
            if (flag > 0) {
                UserNameMsg = "已被注册";
            } else {
                UserNameMsg = "可以注册";
            }
        } else {
            UserNameMsg = "不能为空";
        }
        return UserNameMsg;
    }

    //注册判断 用户名是否被注册 手机号是否被注册
    @RequestMapping(value = "/userPhoneTest", produces = {"application/test;charset=UTF-8"})
    public String registerUserPhoneTest(@Param("userName") String userName, @Param("userPhone") String userPhone) {
        String UserPhoneMsg = "";
        int flag = 0;

        //判断用户名是否存在相同   不能提交空白
        if (userPhone != null && !"".equals(userPhone.trim())) {
            flag = userService.addQueryUser(userName, userPhone);
            if (flag > 0) {
                UserPhoneMsg = "已被注册";
            } else {
                UserPhoneMsg = "可以注册";
            }
        } else {
            UserPhoneMsg = "不能为空";
        }
        return UserPhoneMsg;
    }


    //验证码发送
    @RequestMapping(value = "/getCode")
    public String getCode(@Param("userPhone") String userPhone) {
        String msg = "";

        if (userPhone != null && !"".equals(userPhone.trim())) {
            msg = sendSmsService.verifyCode(userPhone);//将手机号，验证码存入redis
        } else {
            msg = "请输入手机号";
        }


        return msg;
    }

    //验证码验证
    @RequestMapping("/codeStatus")
    public String verifiCodeVerifi(@Param("userPhone") String userPhone,@Param("SmsCode") String SmsCode) {

        String msgCode = "";

        SmsCode = String.valueOf(new Code(SmsCode));//验证码json拼接
        System.out.println(SmsCode);
        //System.err.println(code);
        //System.out.println(code);
        if (SmsCode != null && !"".equals(SmsCode.trim())) {
            //判断验证码和 用户输入的验证码是否相同
            msgCode = sendSmsService.getRedisCode(userPhone,SmsCode);
            if (msgCode == "成功") {
                msgCode = "验证码正确";
            } else if(msgCode == "验证码错误或已失效"){
                msgCode = "验证码错误或已失效";
            }
        } else {
            msgCode = "请输入验证码";
        }

        return msgCode;
    }

}
